#-*-coding:utf-8-*-
 
import string
'''
#结合设计模式里的工厂模式,这个translator更像一个工厂方法,
 
'''
def translator(frm='', to='', delete='', keep=None):
     if len(to) == 1:
          to = to*len(frm)
     trans = string.maketrans(frm,to)
     if keep is not None:
          allchars = string.maketrans('','')
          #这里实际上是把keep从所有的character表里删除了之后
          #返回的character table就是delete
          delete = allchars.translate(allchars,keep.translate(allchars,delete))
     def translate(s):
          return s.translate(trans,delete)
     return translate
 
 
 
digits_only = translator(keep = string.digits)
print digits_only ('chris peekins: 2331-3333')
